import React from 'react';
import {inject, observer} from 'mobx-react';
import {WeaSelect,WeaLocaleProvider} from 'ecCom';
import ViewWeekMonth from '../../../public/viewweekmonth/ViewWeekMonth';
const toJS = mobx.toJS;
const getLabel = WeaLocaleProvider.getLabel;
const getUserLanguage= WeaLocaleProvider.getUserLanguage;
@inject ('planResultListStore')
@observer
export default class DateSelect extends React.Component {
  constructor (props) {
    super (props);
    this.state = {};
    this.store = this.props.planResultListStore;
  }

  componentWillMount () {
    this.store.initYearDateSelectOptions ();
  }

  onDateChange = (param, value) => {
    let {dateSelect} = this.store;
    dateSelect[param] = value;
    this.store.locationkey = new Date ().getTime ();
    this.store.getResultListTable ();
  };

  doClose = val => {
    if (this.store.tabSelectKey == '1') {
      this.onDateChange ('month', val);
    } else {
      this.onDateChange ('week', val);
    }
  };
  onViewDateChange=(totalWeek)=>{
    const {tabSelectKey} = this.store
    const {year,month,week} =this.store.dateSelect
    if(tabSelectKey=='2'){
      if(week > totalWeek){
        this.onDateChange ('week', totalWeek);
      }
    }
  }

  render () {
    const {
      tabSelectKey,
      yearDatas,
      locationkey,
      monthWeekSelectComDisplayKey,
    } = this.store;
    const {dateSelect} = this.store;
    const {year, week, month} = dateSelect;
    let dateLabel = ''
    if(getUserLanguage()==8){
      dateLabel=`${year} ${tabSelectKey=='1'?'Month '+month:'Week '+week}work plan report`
    }else{
      dateLabel=`${year}${getLabel('26577','年')}${(tabSelectKey == '1' ? month + getLabel('33452','月') : week + getLabel('1926','周') )}${getLabel('509004','工作计划报告')}`
    }

    return (
      <div className="year-and-month-contain">
        <WeaSelect ecId={`${this && this.props && this.props.ecId || ''}_WeaSelect@frq6ti`}
          options={toJS (yearDatas)}
          value={year}
          viewAttr={2}
          style={{width: 80, marginRight: 10}}
          onChange={e => {
            this.onDateChange ('year', e);
          }}
        />
        <ViewWeekMonth ecId={`${this && this.props && this.props.ecId || ''}_ViewWeekMonth@q551qz`}
          display={monthWeekSelectComDisplayKey}
          locationkey={locationkey}
          year={year}
          isMonth={tabSelectKey == '1'}
          doClose={this.doClose}
          currentValue={tabSelectKey == '1' ? month : week}
          isshowdate={tabSelectKey != '1'}
          onDateChange={this.onViewDateChange}
        />
        <div style={{display: 'inline', marginLeft: '10px'}}>
          {dateLabel}
        </div>
      </div>
    );
  }
}
